<?php
/**
 * User class file.
 *
 * @author Zhang Jinlong <466028373@qq.com>
 * @link http://www.ijianzhi.cn
 * @copyright Copyright &copy; 2013 DianJin Soft
 */

class User extends CComponent
{
    /**
     * QQ登录用户执行本站点登录
     * @param string $openId
     * @param string $accessToken
     * @param array $userData
     */
    public function loginForQQ($openId, $accessToken, $userData)
    {
        $transaction = Yii::app()->db->beginTransaction();

        try
        {
            $condition = array(
                'condition' => 'openid = :openId',
                'params' => array(':openId' => $openId),
            );

            $userMapQQModel = UserMapQQModel::model()->find($condition);

            /* 用户第一次登陆 */
            if (is_null($userMapQQModel))
            {
                /* 会员主表 */
                $userMainModel = new UserMainModel();

                $userMainModel->username = $userData['nickname'];
                $userMainModel->gender = $userData['gender'] === '女' ? 1 : 2;
                $userMainModel->avatar = $userData['figureurl_2'];

                $userMainModel->save(FALSE);


                /* 联系方式 */
                $userContactModel = new UserContactModel();

                $userContactModel->uid = $userMainModel->uid;

                $userContactModel->save(FALSE);


                /* 关联表 */
                $userMapQQModel = new UserMapQQModel();

                $userMapQQModel->uid = $userMainModel->uid;
                $userMapQQModel->openid = $openId;
                $userMapQQModel->token = $accessToken;

                $userMapQQModel->save(FALSE);
            }
            else
            {
                /* 会员主表 */
                $condition = array(
                    'condition' => 'uid = :uid',
                    'params' => array(':uid' => $userMapQQModel->uid),
                );

                $userMainModel = UserMainModel::model()->find($condition);

                $userMainModel->username = $userData['nickname'];
                $userMainModel->gender = $userData['gender'] === '女' ? 1 : 2;
                $userMainModel->avatar = $userData['figureurl_2'];

                $userMainModel->save(FALSE);


                /* 关联表 */
                $userMapQQModel->openid = $openId;
                $userMapQQModel->token = $accessToken;
                $userMapQQModel->save(FALSE);
            }

            /* 登录日志 */
            $this->addLoginLog($userMainModel->uid);

            /* 用户登录凭证 */
            $this->createCredentials($userMainModel, $userMapQQModel);

            /* 事务提交 */
            $transaction->commit();

            return TRUE;
        }
        catch (Exception $e)
        {
            $transaction->rollBack();

            Yii::log(Helper::generateLogMessage($e), CLogger::LEVEL_ERROR);

            return FALSE;
        }
    }

    /**
     * 写入登录日志
     * @param integer $uid
     */
    public function addLoginLog($uid)
    {
        $logLoginModel = new LogLoginModel();

        $logLoginModel->uid = $uid;

        $logLoginModel->save(FALSE);
    }

    /**
     * 创建用户的登录凭证
     * @param UserMain $userMainModel
     * @param UserMapQQ $userMapQQModel
     */
    public function createCredentials($userMainModel, $userMapQQModel)
    {
        $userIdentity = new UserIdentity($userMainModel->username, '');

        $userIdentity->setState('uid', $userMainModel->uid);
        $userIdentity->setState('username', $userMainModel->username);
        $userIdentity->setState('name', $userMainModel->name);
        $userIdentity->setState('avatar', $userMainModel->avatar);
        $userIdentity->setState('openid', $userMapQQModel->openid);
        $userIdentity->setState('token', $userMapQQModel->token);

        Yii::app()->user->login($userIdentity, 0);
    }
}